define(['angularAMD'], function (angularAMD) {
    angularAMD.service('B0222Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog) {
            this.init = function (parent_scope, callback) {
                parent_scope.oepnB0222 = function(event, plotItem){
                    event.stopPropagation();
                    var content = plotItem;
                    PlotnextopinfoGet(content, function(data){
                        openDialog(data, plotItem);
                    });
                };

                //可用模具查詢
                function PlotnextopinfoGet(content, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.plot_next_op_info_get',
                        content : content,
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                var openDialog = function(data, plotItem){
                    $mdDialog.dialog(
                        'module/moduleB/program/B02/B0222/B0222.html',
                        function (dialog) {
                            return {
                                css_url: 'module/moduleB/program/B02/B0222/B0222.css',
                                flow_datas: data,
                                plotItem:plotItem,
                                back: function () {
                                    dialog.hide();
                                    if(callback)
                                        callback();
                                }
                            };
                        }
                    );
                };

                function zoom(magnification){
                    var kmiImgPanel = document.getElementById('kmi-img-panel'),
                        kmiImgContent = document.getElementById('kmi-img-content');
                    var after_width = kmiImgPanel.width*magnification,
                        after_height = kmiImgPanel.height*magnification;
                    var after = [(after_width-kmiImgPanel.width)/2, (after_height-kmiImgPanel.height)/2];
                    kmiImgPanel.style.width = after_width + 'px';
                    kmiImgPanel.style.height = after_height + 'px';
                    kmiImgContent.scrollLeft = kmiImgContent.scrollLeft+after[0];
                    kmiImgContent.scrollTop = kmiImgContent.scrollTop+after[1];
                }

                parent_scope.oepnB0224 = function(event, image_url){
                    event.stopPropagation();
                    if(image_url && image_url!=''){
                        if(image_url.indexOf('file://') != -1){
                            image_url = image_url.replace('file://', '');
                        } else if(image_url.indexOf('http://') == -1){
                            image_url = 'http://'+config.server.ip+':'+ config.server.port + image_url+ '?hash=' + moment().format('YYYYMMDDHHmmss');
                        }
                        $mdDialog.dialog(
                            'module/moduleB/program/B02/B0224/B0224.html',
                            function (dialog) {
                                return {
                                    image_url: image_url,
                                    css_url:'module/moduleB/program/B02/B0224/B0224.css',
                                    back: function () {
                                        dialog.hide();
                                    },
                                    zoom_in : function(){
                                        zoom(1.1);
                                    },
                                    zoom_out : function(){
                                        zoom(0.9);
                                    }
                                };
                            }
                        );
                    } else {
                        $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                    }
                };
            };
        }]);
});
